CREATE PROCEDURE [util].[SearchErrorLog](
	@SearchString nvarchar(2048) = NULL
)
AS
BEGIN
	DECLARE @ErrorLog TABLE([LogDate] datetime,[ProcessInfo] nvarchar(128),[Text] nvarchar(max))

	DECLARE @err_log_tmp TABLE([ArchiveNo] int, [CreateDate] nvarchar(24), [Size] int);
	INSERT @err_log_tmp EXEC master.dbo.sp_enumerrorlogs;

	DECLARE C_ERRLOGS CURSOR FAST_FORWARD FOR(
		SELECT CAST(er.ArchiveNo AS sysname) AS 'No' FROM @err_log_tmp er
	)ORDER BY [No] ASC;
	DECLARE @LogNumber int;
	OPEN C_ERRLOGS;
	FETCH NEXT FROM C_ERRLOGS INTO @LogNumber;
	WHILE(@@FETCH_STATUS = 0)
	BEGIN
		/* sp_readerrorlog input parameters:
		1.1.       Error log file you want to read: 0 = Actual, 1 = Archive #1, 2= Archive #2, and so on.
		2.2.       Type of error log you want to read: 1 or NULL = SQL Error Log, 2= SQL Agent.
		3.3.       String Lookup 1; string of characters to filter the show results.
		4.4.       String Lookup 2; secondary string of characters to filter the show results, an do a precise lookup
		5.5.       Beginning Date/Hour lookup: to look from this date and hour
		6.6.       Ending Date/Hour lookup: to look until this date and hour
		7.7.       Results Order: ‘asc’= ascendant, ‘desc’= descendent
		*/

		INSERT INTO @ErrorLog 
		EXEC master.sys.sp_readerrorlog @LogNumber, 1, @SearchString;
	
		FETCH NEXT FROM C_ERRLOGS INTO @LogNumber;
	END
	CLOSE C_ERRLOGS;
	DEALLOCATE C_ERRLOGS;
				
	SELECT * FROM @ErrorLog
END
GO
